package com.itcv.mybatis.demo;

import com.itcv.mybatis.demo.dao.MatchRulesMapper;
import com.itcv.mybatis.demo.model.MatchRules;
import com.itcv.mybatis.demo.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.util.MapUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 生命周期示例Main
 */
public class LifeMain {

    public static void main(String[] args) {
        SqlSession sqlSession = null;
        Map<String,Object> map = new HashMap<>();
        MapUtil.computeIfAbsent(map,"hello",k ->{
           System.out.println(k);
            return null;
        });
        try {
            sqlSession = SqlSessionFactoryUtil.openSqlSession();
            MatchRulesMapper matchRulesMapper = sqlSession.getMapper(MatchRulesMapper.class);
            MatchRules matchRules = new MatchRules();
            matchRules.setRuleField("name3");
            matchRules.setRuleName("测试");
            matchRules.setRuleState(new Byte("1"));
            matchRules.setRuleType(3);
            matchRules.setSortNum(6);
            matchRules.setYn(new Byte("1"));
            matchRules.setCreateTime(new Date());
            matchRules.setUpdateTime(new Date());
            matchRulesMapper.insert(matchRules);
            System.out.println(matchRules.getId());
            matchRulesMapper.deleteByPrimaryKey(5);
            sqlSession.commit();
        } catch (Exception e) {
            System.err.println(e.getMessage());
            sqlSession.rollback();
        } finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
    }
}
